import time

from django.http import HttpRequest
from django.shortcuts import render,redirect

# Create your views here.
from Seller import models
from Seller.utils import encrypt, defDecorator


def register(request:HttpRequest):
    if request.method == 'POST':
        username = request.POST.get("username")
        nickname = request.POST.get("nickname")
        password = request.POST.get("password")

        password = encrypt(password)

        models.Seller.objects.create(name = username,nickname=nickname,password=password)

        print("username:",username)
        print("nickname:", nickname)
        print("password:", password)

        return redirect('/Seller/login')


    return render(request,'Seller/register.html')



def login(request:HttpRequest):
    errorMsg=''
    if request.method == 'POST':
        username = request.POST.get("username")
        password = request.POST.get("password")

        password = encrypt(password)

        #数据库查询是否包含该用户或者密码是否正确
        result = models.Seller.objects.filter(name = username,password=password)

        if result:
            Seller = result.last()
            request.session['username'] = Seller.name
            request.session['id'] = Seller.id
            request.session['nickname'] = Seller.nickname
            print(time.localtime(time.time()))
            request.session["time"] = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
            return redirect('/Seller/index')
        else:
            errorMsg ='用户账户不存在或者密码错误'


    return render(request, 'Seller/login.html',{'error_msg':errorMsg })


@defDecorator
def index(request:HttpRequest):
    return render(request, 'Seller/index.html')



def logout(request):
    request.session.flush()
    return redirect('/Seller/login')

def goods_add(request):


    return render(request, 'Seller/good_change.html')
def type_add(request):
    if request.method == 'POST':
        type_name = request.POST.get('type_name')
        models.Type.objects.create(name=type_name)
    return render(request, 'Seller/type_add.html')



@defDecorator
def type_delete(request):
    if request.method == 'GET':
       id = request.GET.get('id')
       models.Type.objects.filter(id=id).delete()
    return redirect('/Seller/type_list/')

@defDecorator
def type_change(request):
    if request.method == 'GET':
        id = request.GET.get('id')
        type_obj = models.Type.objects.get(id=id)
        return render(request, 'seller/type_change.html', {'type_obj':type_obj})
    else:
        id = request.POST.get('id')
        type_name = request.POST.get('type_name')
        models.Type.objects.filter(id=id).update(name=type_name)
        return  redirect('/Seller/type_list/')

@defDecorator
def type_list(request):
    type_obj_list = models.Type.objects.all()
    return render(request, 'seller/type_list.html',{'type_obj_list': type_obj_list})



def goods_add(request):
    seller_id=request.session.get('id')
    type_obj_list = models.Type.objects.all()
    if request.method == 'POST':
        goods_num = request.POST.get("goods_num")
        goods_name = request.POST.get("goods_name")
        goods_oprice = request.POST.get("goods_oprice")
        goods_xprice = request.POST.get("goods_xprice")
        goods_kucun = request.POST.get("goods_kucun")
        goods_sales = request.POST.get("goods_sales")
        goods_descriptopn = request.POST.get("goods_description")
        userfiles = request.POST.get('userfiles')
        goods_type = request.POST.get("goods_type")
        goods_content = request.POST.get("goods_content")

        goodsObject = models.Goods(
            goods_num=goods_num,
            goods_name=goods_name,
            goods_oprice=goods_oprice,
            goods_cprice=goods_xprice,
            goods_kucun=goods_kucun,
            goods_description=goods_descriptopn,
            goods_detail=goods_content,
            goods_sales=goods_sales,
            types=models.Type.objects.get(id=goods_type),
            seller=models.Seller.objects.get(id=seller_id)
        )

        goodsObject.save()

        for index, imgObj in enumerate(userfiles):
            print(index)
            print(imgObj)
            name = imgObj.name.rsplit('.', 1)[1]
            print("name:", name)
            path = 'Seller/static/seller/images/{}_{}.{}'.format(goods_name, index, name)
            print(path)
            with open(path, mode='wb') as f:
                for content in imgObj.chunks():
                    f.write(content)

            imgpath = 'seller/images/{}_{}.{}'.format(goods_name, index, name)

            img_obj = models.Image(
                img_address=imgpath,
                img_label=imgObj.name,
                goods=goodsObject
            )

            img_obj.save()

    return render(request, 'Seller/goods_add.html', locals())
def goods_list(request):
    return  render(request, 'Seller/good_list.html',locals())
def goods_list(request):
    sellerId = request.session.get('id')
    goods_obj_list = models.Goods.objects.filter(seller_id=sellerId)
    return render(request, 'Seller/good_list.html',locals())
@defDecorator
def goods_delete(request):
    if request.method == 'GET':
        id =
